package com.guli.product.app;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;
import com.guli.product.entity.SpuInfoEntity;
import com.guli.product.service.SpuInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.guli.product.entity.SkuInfoEntity;
import com.guli.product.service.SkuInfoService;


/**
 * sku信息
 * @author zzb
 * @email 1626131069@qq.com
 * @date 2023-10-21 10:18:56
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private SpuInfoService spuInfoService;

    @GetMapping("/spuby/{skuId}")
    public R getSpuInfoBySkuId(@PathVariable Long skuId) {
        SkuInfoEntity skuInfoEntity = skuInfoService
                .getOne(new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId));
        Long spuId = skuInfoEntity.getSpuId();
        SpuInfoEntity spuInfoServiceById = spuInfoService.getById(spuId);
        return R.ok().put("data", spuInfoServiceById);
    }


    @GetMapping("/{skuId}/weight")
    public R getWeight(@PathVariable Long skuId) {
        BigDecimal weight = skuInfoService.getWeight(skuId);
        return R.ok().put("data", weight);
    }

    @GetMapping("/{skuId}/price")
    public R getPrice(@PathVariable Long skuId) {
        SkuInfoEntity byId = skuInfoService.getById(skuId);
        return R.ok().put("data", byId.getPrice().toString());
    }

    @GetMapping("/infos/{skuids}")
    @ApiOperation(value = "根据skuids查询所有sku")
    public R skuInfos(@PathVariable List<Long> skuids) {
        List<SkuInfoEntity> skuInfoEntities = skuInfoService.skuInfos(skuids);
        return R.ok().put("data", skuInfoEntities);
    }

    /**
     * 显示sku信息列表
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = skuInfoService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 根据skuId查询sku信息
     */
    @GetMapping("/info/{skuId}")
    public R skuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SkuInfoEntity skuInfo) {
        Boolean isSaved = skuInfoService.handleSaveSkuInfo(skuInfo);
        return R.ok().put("data",isSaved);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SkuInfoEntity skuInfo) {
        Boolean isUpdated = skuInfoService.handleUpdateSkuInfo(skuInfo);
        return R.ok().put("data",isUpdated);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] skuIds) {
        skuInfoService.removeByIds(Arrays.asList(skuIds));
        return R.ok();
    }

}
